#include <QObject>
#include <QTimer>
#include <QDebug>
#include <QDebug>
#include <QPluginLoader>
#include "QCoreApplication"

#include "unionhmidll.h"

UnionHmiDLL::UnionHmiDLL(){
    this->UnionHmiTimerInit();
    connect(HmiFastTimer, SIGNAL(timeout()), this, SLOT(UnionHmiFastTimerUpdate()));
    connect(HmiSlowTimer, SIGNAL(timeout()), this, SLOT(UnionHmiSlowTimerUpdate()));

    this->UnionHmiMainWindowInit();   //注册界面线程

    this->UnionHmiServerInit();
    this->UnionHmiRegHmiControlller2ServerTread();
    this->UnionHmiTransHmiServerHandle2HmiThread();

    this->UnionHmiDLLMainWindowShow();         //UnionSystemDLL初始化完成后，开始正式工作
    this->UnionHmiTimerStart();
}

UnionHmiDLL::~UnionHmiDLL(){
    this->UnionHmiTimerRelease();
    this->UnionHmiServerRelease();
    this->UnionHmiMainWindowRelease();     //注销界面线程
}

short UnionHmiDLL::UnionHmiMainWindowInit(){
    this->pHmiThread = new HmiThread();
    return 1;
}

short UnionHmiDLL::UnionHmiMainWindowRelease(){
    if(pHmiThread != nullptr)
        delete pHmiThread;
    return 1;
}

short UnionHmiDLL::UnionHmiServerInit(){
    pHmiServer = new HmiServer();
    return 1;
}
short UnionHmiDLL::UnionHmiServerRelease(){
    if(pHmiServer != nullptr)
        delete pHmiServer;
    return 1;
}

short UnionHmiDLL::UnionHmiDLLMainWindowShow(){
    this->pHmiThread->show();
    return 1;
}

short UnionHmiDLL::UnionHmiMainWindowWriteProgressBar(int value){
    this->pHmiThread->WriteProgressBar(value);
    return 1;
}

short UnionHmiDLL::UnionHmiMainWindowWriteMessageBox(MessageType Type,const QString Data){
    this->pHmiThread->WriteMessageBox(Type,Data);
    return 1;
}

short UnionHmiDLL::UnionHmiRegHmiControlller2ServerTread(){
    if(this->pHmiThread->pRobControllerHMI != nullptr)
        this->pHmiServer->pRobControllerHMI = this->pHmiThread->pRobControllerHMI;
    if(this->pHmiThread->pTecControllerHMI != nullptr)
        this->pHmiServer->pTecControllerHMI = this->pHmiThread->pTecControllerHMI;
    return 1;
}

short UnionHmiDLL::UnionHmiTransHmiServerHandle2HmiThread(){
    if(this->pHmiServer != nullptr){
        this->pHmiThread->GetHmiServerHandle(this->pHmiServer);
        return 1;
    }
    return 0;
}

short UnionHmiDLL::UnionHmiTimerInit(){
    this->HmiFastTimer = new QTimer();  //高速定时器
    this->HmiSlowTimer = new QTimer();  //低速定时器
    return 1;
}
short UnionHmiDLL::UnionHmiTimerRelease(){
    delete HmiFastTimer;
    delete HmiSlowTimer;
    return 1;
}
short UnionHmiDLL::UnionHmiTimerStart(){
    this->HmiFastTimer->start(50);
    this->HmiSlowTimer->start(500);
    return 1;
}

void UnionHmiDLL:: UnionHmiFastTimerUpdate(){
    if(this->pHmiThread != nullptr)
        this->pHmiThread->FastTimerUpdate();
}

void UnionHmiDLL::  UnionHmiSlowTimerUpdate(){
    if(this->pHmiThread != nullptr)
        this->pHmiThread->SlowTimerUpdate();
}


